package com.wx.mall.sys.controller;


import com.wx.mall.common.utils.DataResult;
import com.wx.mall.sys.service.IAreaService;
import com.wx.mall.sys.vo.req.AreaReqVO;
import com.wx.mall.sys.vo.resp.AreaRespVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.NotNull;
import java.util.List;

/**
 * <p>
 *  区域设置前端控制器
 * </p>
 *
 * @author ljl
 * @since 2020-03-19
 */
@RestController
@RequestMapping("/area")
@Api(tags = "区域设置相关接口")
public class AreaController {

    @Autowired
    private IAreaService areaService;

    @PostMapping("/addArea")
    @ApiOperation("新增区域")
    public DataResult add(@ApiParam("区域vo")@Valid AreaReqVO vo) {
        areaService.add(vo);
        return DataResult.success();
    }

    @GetMapping("/areas")
    @ApiOperation("获取所有区域列表")
    public DataResult findAll() {
        List<AreaRespVO> areas = areaService.getAreas();
        return DataResult.success(areas);
    }


    @GetMapping("/areas/{tier}")
    @ApiOperation("根据tier获取顶级到tier层级的所有区域列表")
    public DataResult findAllByTier(@ApiParam("区域层级tier") @PathVariable("tier") @NotNull(message = "区域tier不能为空") Integer tier) {
        List<AreaRespVO> areas = areaService.getAreasByTier(tier);
        return DataResult.success(areas);
    }
}

